package download.itydl.com.sinlethread.mutilthread.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.List;

import download.itydl.com.sinlethread.R;
import download.itydl.com.sinlethread.mutilthread.bean.FileInfo;
import download.itydl.com.sinlethread.mutilthread.services.DownloadService;

/**
 * 多线程下载UI层显示适配器
 *
 * @author howard
 */
public class MultiDownloadAdapter extends BaseAdapter {

    private Context mContext;
    private List<FileInfo> mFileInfoList;

    public MultiDownloadAdapter(Context mContext, List<FileInfo> mFileInfoList) {
        this.mContext = mContext;
        this.mFileInfoList = mFileInfoList;
    }

    @Override
    public int getCount() {
        return mFileInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return mFileInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final FileInfo fileInfo = mFileInfoList.get(position);
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_list, null);
            holder.mTvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
            holder.mProgressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
            holder.mDownload = (Button) convertView.findViewById(R.id.download);
            holder.mPause = (Button) convertView.findViewById(R.id.pause);
            holder.mTvPrgoress = (TextView) convertView.findViewById(R.id.tvPrgoress);
            holder.mTvTitle.setText(fileInfo.getFileName());
            holder.mProgressBar.setMax(100);

            // 一条Item代表一个文件，开启它的下载，可能是多线程的
            holder.mDownload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 开始下载
                    Intent intent = new Intent(mContext, DownloadService.class);
                    intent.putExtra(DownloadService.FILE_INFO, fileInfo);
                    intent.setAction(DownloadService.ACTION_START);
                    mContext.startService(intent);
                }
            });
            holder.mPause.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 暂停下载
                    Intent intent = new Intent(mContext, DownloadService.class);
                    intent.putExtra(DownloadService.FILE_INFO, fileInfo);
                    intent.setAction(DownloadService.ACTION_PAUSE);
                    mContext.startService(intent);
                }
            });

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.mProgressBar.setProgress((int) fileInfo.getFinished());
        holder.mTvPrgoress.setText(fileInfo.getFinished() + "%");

        return convertView;
    }

    static class ViewHolder {
        TextView mTvTitle;
        ProgressBar mProgressBar;
        Button mDownload;
        Button mPause;
        TextView mTvPrgoress;
    }

    public void upDataProgress(int position, long progress) {
        // 修改数据源
        mFileInfoList.get(position).setFinished(progress);
        // 刷新视图，更新进度
        notifyDataSetChanged();
    }
}
